﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Common.ParameterLocator;
using NUnit.Framework;
using SpecFlowTfsLinker.Entities;
using SpecFlowTfsLinker.Parser;
using SpecFlowTfsLinker.Tools;

namespace SpecFlowTfsLinker.Printer
{
    public class StatementPrinter
    {
        private readonly Statement _statement;
        private string _parameteredStatement;
        public string DisplayStatement
        {
            get
            {
                return StatementTools.DeleteAllQuotes(_statement);
            }
        }


        public string DisplayMethod
        {
            get { return string.Format("public void {0}({1})", _statement.Value.ToPascalCase(), DisplayParameters); }
        }

        public StatementPrinter(Statement statement)
        {
            _statement = statement;
            DisplayType = statement.Type;
            Parameters = ParameterParser.ExtractParametersFromStatement(_statement.Value);
        }

        public StatementPrinter(Statement statement, StatementType displayType)
            : this(statement)
        {
            DisplayType = displayType;
        }

        public StatementType DisplayType { get; private set; }

        public string DisplayParameters
        {
            get
            {
                var builder = new StringBuilder();
                int parameterIndex = 1;
                Parameters.ForEach(parameter =>
                                                 {
                                                     builder.AppendFormat(ServiceParameter.Instance.Parameter.FormatDisplayParameter, parameter.DisplayType, parameterIndex);
                                                     if (Parameters.Last() != parameter)
                                                         builder.Append(", ");
                                                     parameterIndex++;
                                                 });
                return builder.ToString();
            }
        }

        public string DisplayMethodStatement
        {
            get
            {
                if (string.IsNullOrEmpty(_parameteredStatement))
                    _parameteredStatement = StatementTools.TransformStatementToStatementWithParameter(_statement, Parameters);
                return _parameteredStatement;
            }
        }

        public List<ParameterPrinter> Parameters { get; private set; }
    }
}